﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CST_Dims.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		Dialog for 3D points settings
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports System.Windows.Forms
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.DatabaseServices

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '




'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '

Public Class CST_Dims

    Private m_SortingColumn As ColumnHeader
    Private locSty() As DIMSTY

    '// --------------------------------------------------------------------- '
    Private Sub Reload_CB_Style()
        Dim stmp As String = Me.CB_Style.Text
        Me.CB_Style.Items.Clear()
        If (locSty Is Nothing) Then Return
        If (locSty.Length > 0) Then
            For Each st As DIMSTY In locSty
                Dim i As Integer
                i = Me.CB_Style.Items.Add(st.sName)
                If (stmp IsNot Nothing) Then
                    If (st.sName = stmp) Then
                        Me.CB_Style.SelectedIndex = i
                        stmp = Nothing
                    End If
                End If
            Next
            If String.IsNullOrEmpty(stmp) = False Then
                Me.CB_Style.SelectedIndex = 0
            End If
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub OK_Button_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) _
                                Handles OK_Button.Click

        If (CMain.GetTxDbl(Me.TX_DimScl, CM_Dims.Stgs.dDimScale) = False) Then
            Return
        End If
        CM_Dims.SavedSty = locSty
        If (Me.CB_Style.Items.Count > 0) Then
            CM_Dims.Stgs.iCrtSty = Me.CB_Style.SelectedIndex
        Else
            CM_Dims.Stgs.iCrtSty = 0
        End If
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles Cancel_Button.Click

        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CST_Dims_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load


        Me.Text = CMain._T(TX.DIMS) & ". " & _
                CMain._T(TX.SETTINGS) & "."
        Me.OK_Button.Text = CMain._T(TX.OK)
        Me.Cancel_Button.Text = CMain._T(TX.CANCEL)
        Me.TP_General.Text = CMain._T(TX.GENERAL)

        Me.L_DimScale.Text = CMain._T(TX.IMDB_DIMSCL)

        Me.TX_DimScl.Text = CM_Dims.Stgs.dDimScale.ToString("0.0000")

        Me.B_AddSty.Text = CMain._T(TX.DIM_ADDSTY)
        Me.B_AddDim.Text = CMain._T(TX.DIM_ADDDIM)
        Me.B_Rename.Text = CMain._T(TX.DIM_RENM)
        Me.B_FavRem.Text = CMain._T(TX.REMOVE)


        '// Set-up the list of favorite images
        Me.LW_Fav.CheckBoxes = False
        Me.LW_Fav.Columns.Add(CMain._T(TX.NAME), 180)

        Dim i As Integer = 0
        If (CM_Dims.Stgs.iCrtSty = -1) Then CM_Dims.Stgs.iCrtSty = 0
        Dim li As System.Windows.Forms.ListViewItem
        If (CM_Dims.SavedSty IsNot Nothing) Then
            For Each sE As DIMSTY In CM_Dims.SavedSty
                li = Me.LW_Fav.Items.Add(sE.sName)
                Me.CB_Style.Items.Add(sE.sName)
                i = i + 1
            Next
            Me.CB_Style.SelectedIndex = CM_Dims.Stgs.iCrtSty
        End If
        locSty = CM_Dims.SavedSty
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_AddSty_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles B_AddSty.Click

        Dim bOk As Boolean = False
        Try
            Dim acDoc As ApplicationServices.Document = _
                ApplicationServices.Application.DocumentManager.MdiActiveDocument
            If (acDoc Is Nothing) Then Return
            Me.LW_Fav.SelectedItems.Clear()
            CTXM_ListOfSty.Items.Clear()

            Using acLock As ApplicationServices.DocumentLock = acDoc.LockDocument
                Using acTrans As Transaction = _
                    acDoc.Database.TransactionManager.StartTransaction()
                    Try
                        '// get dimension style table
                        Dim acDimStyTbl As DimStyleTable = DirectCast( _
                                acTrans.GetObject(acDoc.Database.DimStyleTableId, _
                                                  OpenMode.ForRead),  _
                                DimStyleTable)
                        For Each oId As ObjectId In acDimStyTbl
                            '// open the style to be able to read it's properties
                            Dim acDimSty As DimStyleTableRecord = DirectCast( _
                                    acTrans.GetObject(oId, _
                                                      OpenMode.ForRead),  _
                                    DimStyleTableRecord)
                            CTXM_ListOfSty.Items.Add(acDimSty.Name)
                        Next
                        bOk = True
                        acTrans.Commit()
                    Catch ex As System.Exception
                        acTrans.Abort()
                        CMain.DebugException(ex)
                        CMain.PopUpExc()
                    End Try
                End Using
            End Using
            If bOk = True Then

                CTXM_ListOfSty.Show(Me.B_AddSty, New System.Drawing.Point( _
                                    0, _
                                    Me.B_AddSty.Size.Height))
            End If

        Catch ex As Exception
            CMain.DebugException(ex)
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_AddDim_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles B_AddDim.Click
        Me.Hide()
        Try
            Me.LW_Fav.SelectedItems.Clear()
            Dim acDoc As ApplicationServices.Document = _
                ApplicationServices.Application.DocumentManager.MdiActiveDocument
            If (acDoc Is Nothing) Then Return
            Using acLock As ApplicationServices.DocumentLock = acDoc.LockDocument

                Dim li As System.Windows.Forms.ListViewItem
                Dim sOut As DIMSTY = Nothing
                Dim sErrStr As String = Nothing
                If (CM_Dims.DimStyFromDim(acDoc, sOut, sErrStr) = True) Then
                    CM_Dims.DimStyAdd(locSty, sOut)
                    li = Me.LW_Fav.Items.Add(sOut.sName)
                    li.Selected = True
                    Me.CB_Style.Items.Add(sOut.sName)
                Else
                    Autodesk.AutoCAD.ApplicationServices.Application. _
                        ShowAlertDialog(sErrStr)
                End If

            End Using
            GC.Collect()
            GC.WaitForPendingFinalizers()
        Catch ex As Exception
            CMain.DebugException(ex)
        End Try
        Me.Show()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Rename_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles B_Rename.Click

        Try

            If (Me.LW_Fav.Items.Count = 0) Then Return
            If (LW_Fav.SelectedItems.Count = 0) Then
                Autodesk.AutoCAD.ApplicationServices.Application. _
                    ShowAlertDialog(CMain._T(TX.NOSELLI))
                Return
            End If

            Dim sNewName As String = ""
            Dim iIdx As Integer
            Dim sTmp As String = LW_Fav.SelectedItems(0).Name
            sNewName = InputBox(CMain._T(TX.ADD), CMain._T(TX.DIM_RENM), _
                                LW_Fav.SelectedItems(0).Name)
            If (String.IsNullOrEmpty(sNewName)) Then
                Exit Try
            End If
            If (LW_Fav.SelectedItems.Count > 1) Then
                Dim sSecTmp As String
                For i As Integer = 0 To LW_Fav.SelectedItems.Count - 1
                    If (CM_Dims.DimStyHas(locSty, LW_Fav.SelectedItems(i).Text, iIdx)) Then
                        sSecTmp = sNewName & _
                            "(" & (i + 1).ToString() & ")"
                        locSty(iIdx).sName = sSecTmp
                        LW_Fav.SelectedItems(i).Text = sSecTmp
                    End If
                Next
            Else
                If (CM_Dims.DimStyHas(locSty, LW_Fav.SelectedItems(0).Text, iIdx)) Then
                    If (Me.CB_Style.Text = sTmp) Then
                        Me.CB_Style.Text = sNewName
                    End If
                    locSty(iIdx).sName = sNewName
                    LW_Fav.SelectedItems(0).Text = sNewName
                End If
            End If
            Reload_CB_Style()

        Catch ex As Exception
            CMain.DebugException(ex)
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_FavRem_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles B_FavRem.Click

        If (Me.LW_Fav.Items.Count = 0) Then Return
        If (LW_Fav.SelectedItems.Count = 0) Then
            Autodesk.AutoCAD.ApplicationServices.Application. _
                ShowAlertDialog(CMain._T(TX.NOSELLI))
            Return
        End If

        For Each li As System.Windows.Forms.ListViewItem In LW_Fav.SelectedItems
            Dim iIdx As Integer
            Try
                If (CM_Dims.DimStyHas(locSty, li.Text, iIdx)) Then
                    CM_Dims.DimStyRem(locSty, iIdx)
                    li.Remove()
                End If
            Catch ex As Exception
                Debug.Print("Rare error! Non-numeric value on style list index column")
            End Try
        Next li
        Reload_CB_Style()
        If (LW_Fav.Items.Count > 0) Then
            LW_Fav.Items(0).Selected = True
        End If
        GC.Collect()
        GC.WaitForPendingFinalizers()

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub LW_Fav_ColumnClick( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.ColumnClickEventArgs) _
        Handles LW_Fav.ColumnClick

        '// code copied from
        '// http://www.vb-helper.com/howto_net_listview_sort_clicked_column.html

        ' Get the new sorting column.
        Dim new_sorting_column As ColumnHeader = _
            Me.LW_Fav.Columns(e.Column)

        ' Figure out the new sorting order.
        Dim sort_order As System.Windows.Forms.SortOrder
        If m_SortingColumn Is Nothing Then
            ' New column. Sort ascending.
            sort_order = SortOrder.Ascending
        Else
            ' See if this is the same column.
            If new_sorting_column.Equals(m_SortingColumn) Then
                ' Same column. Switch the sort order.
                If m_SortingColumn.Text.StartsWith("^") Then
                    sort_order = SortOrder.Descending
                Else
                    sort_order = SortOrder.Ascending
                End If
            Else
                ' New column. Sort ascending.
                sort_order = SortOrder.Ascending
            End If

            ' Remove the old sort indicator.
            m_SortingColumn.Text = _
                m_SortingColumn.Text.Substring(2)
        End If

        ' Display the new sort order.
        m_SortingColumn = new_sorting_column
        If sort_order = SortOrder.Ascending Then
            m_SortingColumn.Text = "^ " & m_SortingColumn.Text
        Else
            m_SortingColumn.Text = "v " & m_SortingColumn.Text
        End If

        ' Create a comparer.
        Me.LW_Fav.ListViewItemSorter = New  _
            ListViewComparer(e.Column, sort_order)

        ' Sort.
        Me.LW_Fav.Sort()

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CTXM_ListOfSty_ItemClicked( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) _
        Handles CTXM_ListOfSty.ItemClicked

        Try
            Dim acDoc As ApplicationServices.Document = _
                ApplicationServices.Application.DocumentManager.MdiActiveDocument
            If (acDoc Is Nothing) Then Return
            Me.LW_Fav.SelectedItems.Clear()
            Using acLock As ApplicationServices.DocumentLock = acDoc.LockDocument
                Dim sOut As DIMSTY = Nothing
                Dim sErrStr As String = Nothing
                Dim li As System.Windows.Forms.ListViewItem
                If (CM_Dims.DimStyFromSty(acDoc, e.ClickedItem.Text, sOut, sErrStr) = False) Then
                    Autodesk.AutoCAD.ApplicationServices.Application. _
                        ShowAlertDialog(sErrStr)
                Else
                    CM_Dims.DimStyAdd(locSty, sOut)
                    li = Me.LW_Fav.Items.Add(sOut.sName)
                    li.Selected = True
                    Me.CB_Style.Items.Add(sOut.sName)
                End If
            End Using
        Catch ex As Exception
            CMain.DebugException(ex)
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
        CTXM_ListOfSty.Hide()
    End Sub
    '// ===================================================================== '


End Class

'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
